<?php
/*
 * @author eBIZ Team <dev@jungo.com.cn>
 * @copyright  Copyright (c) , ShenZhen JunGO Technology Co., Ltd. All rights reserved.
 * @license  Commercial authorization, unauthorized use and modification are prohibited
 * @url www.jungo.com.cn
 */

namespace ckeeper\http;


use Selibra\Di\Annotations\Autowired;
use Selibra\Di\Annotations\Component;
use Selibra\Http\Annotations\Controller;
use Selibra\Http\Annotations\Route;
use ckeeper\dao\ManagerDao;
use ckeeper\services\RoleService;
use Selibra\Tools\Console;

/**
 * 账户管理控制器
 * Class Account
 * @package Admin\Account\Controller
 */
#[Component]
#[Controller("/system/account", Account::class)]
class Account extends Http
{

    /**
     * 不需要权限验证
     * @var bool
     */
    protected bool $accessAuth = false;

    /**
     * @var ManagerDao
     */
    #[Autowired]
    protected ManagerDao $managerDao;

    /**
     * @var RoleService
     */
    #[Autowired]
    protected RoleService $roleService;

    /**
     * 账户基础信息
     * @return mixed
     */
    #[Route("/info")]
    public function info()
    {
        $token = $this->getRequest()->getGet('token');
        if (empty($token)) {
            return $this->error("token不可为空", [], 100);
        }
        $managerEntity = $this->managerDao->getManagerAccountByToken($token);
        if( empty($managerEntity) ){
            return $this->error("token无效",null,10002);
        }
        // 获取账户拥有的权限
        $roles = $managerEntity->getRoles();
        $roleIds = array_filter(explode(',',$roles));
        $access = [];
        if( !empty($roleIds) ){
            // 权限
            $access = $this->roleService->getRightsByIds($roleIds);
        }

        return $this->success(null,[
            'uuid' => $managerEntity->uuid,
            'avatar' => $managerEntity->avatar,
            'name' => $managerEntity->name,
            'access' => $access,
        ]);
    }

}
